import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.File;

public class DataBaseUtil{

	private static String url;
	private static String username;
	private static String pwd;

	private static Connection conn = null;

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Properties pro = new Properties();
			pro.load(DataBaseUtil.class.getClassLoader().getResourceAsStream("resources/jdbc.properties"));
			//pro.load(new FileInputStream(new File("resources/jdbc.properties")));
			url = pro.getProperty("jdbc.url");
			username = pro.getProperty("jdbc.user");
			pwd = pro.getProperty("jdbc.pwd");
			conn = DriverManager.getConnection(url, username, pwd);
		} catch(Exception e){
			e.printStackTrace();
		}
		
	}

	public static int add(String sql) throws Exception{
		Statement sm = conn.createStatement();
		return sm.executeUpdate(sql);
	}

	public static BlackBox query(String sql) throws Exception{
		Statement sm = conn.createStatement();
		ResultSet rs = sm.executeQuery(sql);
		BlackBox bb = new BlackBox();

		while (rs.next()) {
					
			Points start = new Points(rs.getInt("start_xpoint"), rs.getInt("start_ypoint"));
			Points end = new Points(rs.getInt("end_xpoint"), rs.getInt("end_ypoint"));
			bb.setStart(start);
			bb.setEnd(end);
			bb.setOrder(rs.getString("order").split(" "));
			bb.setId(rs.getInt("id"));
			
		}

		return bb;
	}
}